/**
  * AFilterAction.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ws4is.engine.actions;


import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ws4is.engine.ConfigObject;
import ws4is.engine.enums.HTTPRequestType;

/*
 * Main action class for implementing Filter AActions for Filters
 */
public abstract class AFilterAction extends ABaseAction{
	
	protected transient FilterConfig sc ; 
	
    public void setFilterConfig(FilterConfig sc) {
		this.sc = sc;
	}
	    
    protected final void callMethod(final String cmd, final ConfigObject config, final HTTPRequestType type , final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws Exception, IOException {
    	setSetters(type, request, response);
		Exception ex = null;
		try{
			if (cmd==null) {
				if (defaultReponse(cmd,request , response)){
					chain.doFilter(request, response);
				}
				return;
			}
			Method m = getMethod(config, cmd);
			Object[] params = { request , response};
			Boolean o = (Boolean)m.invoke(this, params);
			
			if (o.booleanValue()){
				chain.doFilter(request, response);
			}
			closePrintWriter();
		}catch(NoSuchMethodException exc){
			if (defaultReponse(cmd,request , response)){
				chain.doFilter(request, response);
			}
		}catch(Exception e){
			ex=e;
			throw e;
		}finally{
			postExecute(type,cmd,ex);
		}
    }
    
    
 
	
}
